package com.eveinfo.display;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.api.utils.skilllist.ApiSkillGroup;
import com.eveinfo.api.utils.skilllist.ApiSkills;
import com.eveinfo.tools.ActivityLauncher;

public class AllSkillListAdapter extends BaseAdapter {

	private List<ApiSkillGroup> allSkills;
	private Activity activity;
	private static Integer[] unfold;

	public AllSkillListAdapter(final Activity context, ListView listview) {
		this.activity = context;
		ApiSkills allSkill = EveInfo.getData().getSkills();
		this.allSkills = allSkill.getSkillGroups();
		unfold = new Integer[allSkills.size()];
		for (int i = 0; i < allSkills.size(); i++) {
			unfold[i] = 0;
		}
		listview.setAdapter(this);
	}

	@Override
	public Object getItem(int position) {
		int i = 0;
		for (ApiSkillGroup section : allSkills) {
			int size = 1;
			if (unfold[i] == 1) {
				size += section.countSkill();
			}

			if (position == 0)
				return section.getGroupName();
			if (position < size)
				return section.getSkillWithPosition(position - 1).getTypeName();

			position -= size;
		}
		return null;
	}

	@Override
	public int getCount() {
		int total = 0;
		int i = 0;
		for (ApiSkillGroup section : allSkills) {
			if (unfold[i] == 1) {
				total += section.countSkill() + 1;
			} else {
				total++;
			}
			i++;
		}
		return total;
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		int pos = position;
		int i = 0;
		for (ApiSkillGroup section : allSkills) {
			final int y = i;

			int size = 1;
			if (unfold[i] == 1) {
				size += section.countSkill();
			}
			TextView label = new TextView(activity);
			label.setPadding(5, 2, 0, 2);

			if (pos == 0) {
				label.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (unfold[y] == 0) {
							unfold[y] = 1;
						} else {
							unfold[y] = 0;
						}
						notifyDataSetChanged();
					}
				});
				label.setTextSize(20);
				label.setText(section.getGroupName());
				return label;
			}
			if (pos < size) {
				label.setPadding(15, 5, 0, 5);
				final int typeID = section.getSkillWithPosition(pos - 1).getTypeID();
				label.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						ActivityLauncher.displaySkill(activity, typeID, -1);
					}
				});
				label.setClickable(true);
				label.setTextSize(15);
				label.setText(section.getSkillWithPosition(pos - 1).getTypeName());
				return label;
			}

			pos -= size;
			i++;
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}
